package com.gigi.service.manual;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gigi.entity.NoticeShiqinEntity;
import com.gigi.enums.ErrorCodeEnum;
import com.gigi.enums.YesOrNoEnum;
import com.gigi.mapper.generated.NoticeShiqinMapper;
import com.gigi.model.ListRequest;
import com.gigi.model.NoticeDO;
import com.gigi.service.generated.NoticeShiqinService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class NoticeManageService {

    private final NoticeShiqinService noticeShiqinService;
    private final NoticeShiqinMapper noticeShiqinMapper;

    public NoticeManageService(NoticeShiqinService noticeShiqinService, NoticeShiqinMapper noticeShiqinMapper) {
        this.noticeShiqinService = noticeShiqinService;
        this.noticeShiqinMapper = noticeShiqinMapper;
    }

    /**
     * 列表接口
     *
     * @param request
     * @return
     */
    public MultiResponse<NoticeShiqinEntity> list(ListRequest request) {
        if (request.getPageNum() < 1) {
            request.setPageNum(1);
        }
        if (request.getPageSize() < 1) {
            request.setPageSize(10);
        }
        LambdaQueryWrapper<NoticeShiqinEntity> wrapper = new LambdaQueryWrapper<NoticeShiqinEntity>()
                .orderByDesc(NoticeShiqinEntity::getId);

        List<NoticeShiqinEntity> list;
        int total = 0;

        if (YesOrNoEnum.YES.getCode().equals(request.getIfByPage())) {
            Page<NoticeShiqinEntity> page = new Page<>(request.getPageNum(), request.getPageSize());
            Page<NoticeShiqinEntity> noticePage = noticeShiqinMapper.selectPage(page, wrapper);
            list = noticePage.getRecords();
            total = (int) noticePage.getTotal();

        } else {
            list = noticeShiqinService.list(wrapper);
        }
        return MultiResponse.of(list, total);
    }

    /**
     * 详情接口
     *
     * @param id
     * @return
     */
    public SingleResponse<NoticeShiqinEntity> detail(Long id) {
        NoticeShiqinEntity entity = noticeShiqinMapper.selectById(id);
        if (Objects.isNull(entity)) {
            return SingleResponse.buildFailure(ErrorCodeEnum.NO_DATA.getErrCode(), ErrorCodeEnum.NO_DATA.getErrDesc());
        }
        return SingleResponse.of(entity);
    }

    /**
     * 保存接口
     *
     * @param request
     * @return
     */
    public Response save(NoticeDO request) {
        if (ObjectUtil.isNull(request.getId())) {
            NoticeShiqinEntity target = new NoticeShiqinEntity();
            target.setContent(request.getContent());
            if (!noticeShiqinService.save(target)) {
                return Response.buildFailure(ErrorCodeEnum.SAVE_FAILED.getErrCode(), ErrorCodeEnum.SAVE_FAILED.getErrDesc());
            }
        } else {
            NoticeShiqinEntity target = new NoticeShiqinEntity();
            target.setContent(request.getContent());
            target.setId(request.getId());
            if (!noticeShiqinService.updateById(target)) {
                return Response.buildFailure(ErrorCodeEnum.EDIT_FAILED.getErrCode(), ErrorCodeEnum.EDIT_FAILED.getErrDesc());
            }
        }
        return Response.buildSuccess();
    }

    /**
     * 删除接口
     *
     * @param id
     * @return
     */
    public Response delete(Long id) {
        if (!noticeShiqinService.removeById(id)) {
            return Response.buildFailure(ErrorCodeEnum.DELETE_FAILED.getErrCode(), ErrorCodeEnum.DELETE_FAILED.getErrDesc());
        }
        return Response.buildSuccess();
    }

}
